<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
  function request() {
    return new Promise((resolve, reject) => {
      const val = Math.random();
      if (val > 0.9) {
        resolve(val);
      } else {
        reject(val);
      }
    });
  }

  function retry(req, times = 0, delay = 2000) {
    return new Promise((resolve, reject) => {
      const handle = async () => {
        try {
          resolve(await req());
        } catch (err) {
          times--;
          if (times > 0) {
            console.log('请求重试，还剩', times);
            setTimeout(() => {
              handle();
            }, delay)
          } else {
            reject(err);
          }
        }
      }

      handle();
    });
  }

  retry(request, 3, 1000).then((res) => {
    console.log('请求成功', res);
  }).catch((err) => {
    console.log('请求失败', err);
  })
</script>
</body>
</html>